<template>
	<view>
		<pageHeader  @back='back' :fixed='true' ></pageHeader>
		<text class="word">密码登录</text>
		<view class="input">
			<input type="number" placeholder="请输入账号" v-model="name"/>
		</view>
		<view class="input" style="top:407rpx;">
			<input type="password" placeholder="请输入密码"  v-model="password"/>
		</view>
		<view class="button" @tap='toLogin' :style="{backgroundColor:canLogin?'':'#C8C8C8',color:canLogin?'':'#fff'}">登录</view>
	</view>
</template>

<script>
	import url from '@/common/util/personal.js'
	import pageHeader from "@/components/header/header.vue"
	export default {
		components: {pageHeader},
		data() {
			return {
				name:"",
				password:"",
				canLogin:false,
				from:false
			};
		},
		onLoad(e) {
			if(e.index){
				this.from=true
			}
		},
		watch: {
			name(name) {
				setTimeout(()=>{
					if(name>99999999999){
					    this.name=this.name.slice(0, 11)
					}
				},0)
				if(/^1[0-9]{10}$/.test(name)){
				    if(this.password){
				        this.canLogin=true
				    }else{
						this.canLogin=false
					}
				}else{
				    this.canLogin=false
				}
			},
			password(){
				if(/^1[0-9]{10}$/.test(this.name)){
				    if(this.password){
				        this.canLogin=true
				    }else{
						this.canLogin=false
					}
				}else{
				    this.canLogin=false
				}
			}
		},
		methods:{
			back(){
				if(this.from){
					uni.reLaunch({
						url: '/pages/index/index'
					})
					return
				}
				uni.navigateBack({
				    delta: 1
				})
			},
			setCookie(cname, cvalue, exdays) {
			    var d = new Date();
			    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
			    var expires = "expires=" + d.toUTCString();
			    document.cookie = cname + "=" + cvalue + "; " + expires;
			 },
			async toLogin(){
				let res=await url.login({
				        username:this.name,
						password:this.password
				    })
				if(res.code==0){
					localStorage.setItem('x-token',res.data.token) 
					localStorage.setItem("userId",res.data.userId)
					this.setCookie("userId", res.data.userId, 2)
					if(this.from){
						uni.reLaunch({
							url: '/pages/index/index'
						})
						return
					}
					uni.navigateBack({
					    delta: 1
					})
				}	
			}
		}
	}
</script>

<style lang="scss">
.word{
	position: fixed;
	top: 199rpx;
	left: 80rpx;
	font-size: 44rpx;
	line-height: 62rpx;
	color: #333333;
	font-weight: bold;
}
.input{
	position: fixed;
	top: 296rpx;
	left: 80rpx;
	width: 590rpx;
	height: 111rpx;
	display: flex;
	align-items: flex-end;
	border-bottom:1rpx solid #E6E6EB ;
	input{
		font-size:32rpx ;
		color:#333333;
		height: 100rpx;
		width: 100%;
	}
}
.button{
	height:90rpx ;
	width: 590rpx;
	background-color:#EAB313 ;
	border-radius: 45rpx;
	color: #333333;
	font-size: 32rpx;
	line-height: 45rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	position: fixed;
	top:668rpx ;
	left: 80rpx;
}
</style>
